package com.itheima.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.HashMap;
import java.util.Map;

//JWT的生成和解析
public class JwtUtil {
    // 创建token
    public static String createToken(Map claims) {
        return Jwts.builder()
                .setClaims(claims) //设置响应数据体
                .signWith(SignatureAlgorithm.HS256, ConstantUtil.JWT_SECRET) //设置加密方法和加密盐
                .compact();
    }

    // 解析token
    public static Map parseToken(String token) {
        return Jwts.parser().setSigningKey(ConstantUtil.JWT_SECRET)
                .parseClaimsJws(token)
                .getBody();
    }

    public static void main(String[] args) {
        Map resultMap = new HashMap();
        resultMap.put("id","123");
        resultMap.put("phone","13800138000");

        String token = createToken(resultMap);
        System.out.println(token);

        token = "eyJhbGciOiJIUzI1NiJ9.eyJwaG9u1234EzODAwMTM4MDAwIiwiaWQiOiIxMjMifQ.v3v9Cv0X0HnjCfpPWTn_-mZ2_mlZz7chqzYO7jD6PpY";

        Map map = parseToken(token);
        System.out.println(map);
    }
}
